<!DOCTYPE html>
<title>Test fenced frame does not allow call background fetch</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/utils.js"></script>
<script src="/common/get-host-info.sub.js"></script>

<body>

  <script>
    const background_fetch_register_key = KEYS['background-fetch'];
    const fenced_frame_url = 'resources/background-fetch-inner.https.html';

    function base_path() {
      return location.pathname.replace(/\/[^\/]*$/, '/');
    }

    async function test(url, expected, message) {
      attachFencedFrame(url)

      // Get the result for the top-level fenced frame.
      const fenced_frame_result = await nextValueFromServer(background_fetch_register_key);
      assert_equals(fenced_frame_result, expected, message);
    }

      promise_test(async () => {
      const url = `${fenced_frame_url}?fetch`;
      await test(
        url,
        '[backgroundFetch.fetch] Failed inside fencedframe as expected',
        'backgroundFetch.fetch is disallowed inside a same-origin fenced frame');

      const CROSS_ORIGIN_DESTINATION =
        get_host_info()['HTTPS_REMOTE_ORIGIN'] + base_path() + url;
      await test(
        CROSS_ORIGIN_DESTINATION,
        '[backgroundFetch.fetch] Failed inside fencedframe as expected',
        'backgroundFetch.fetch is disallowed inside a cross-origin fenced frame');
    }, 'backgroundFetch.fetch');

      promise_test(async () => {
      const url = `${fenced_frame_url}?get`;
      await test(
        url,
        '[backgroundFetch.get] Failed inside fencedframe as expected',
        'backgroundFetch.get is disallowed inside a same-origin fenced frame');

      const CROSS_ORIGIN_DESTINATION =
        get_host_info()['HTTPS_REMOTE_ORIGIN'] + base_path() + url;
      await test(
        CROSS_ORIGIN_DESTINATION,
        '[backgroundFetch.get] Failed inside fencedframe as expected',
        'backgroundFetch.get is disallowed inside a cross-origin fenced frame');
    }, 'backgroundFetch.get');

      promise_test(async () => {
      const url = `${fenced_frame_url}?getIds`;
      await test(
        url,
        '[backgroundFetch.getIds] Failed inside fencedframe as expected',
        'backgroundFetch.getIds is disallowed inside a same-origin fenced frame');

      const CROSS_ORIGIN_DESTINATION =
        get_host_info()['HTTPS_REMOTE_ORIGIN'] + base_path() + url;
      await test(
        CROSS_ORIGIN_DESTINATION,
        '[backgroundFetch.getIds] Failed inside fencedframe as expected',
        'backgroundFetch.getIds is disallowed inside a cross-origin fenced frame');
    }, 'backgroundFetch.getIds');
  </script>

</body>
